import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  // 加载环境变量
  const env = loadEnv(mode, process.cwd());
  return {
    plugins: [vue()],
    resolve: {
      // ↓路径别名
      alias: {
        '@': resolve(__dirname, './src'),
        '~@': resolve(__dirname, './src')
      },
      // 忽略后缀名的配置选项
      extensions: ['.js', '.ts', '.vue', '.json']
    },
    define: {
      // ↓全局环境变量
      'process.env': env
    },
 
    server: {
      // ↓配置代理
      proxy: {
        '/api': {
          target: env.VITE_API_BASE_URL,
          changeOrigin: true, // 是否跨域
          rewrite: (path) => {
            return path.replace(/^\/api/, '');
          }
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler',  
        },
      },
    }
  };
});